package com.jsz.yun.ai.field.controller;

import cn.hutool.core.bean.BeanUtil;
import com.jsz.yun.ai.field.common.Req;
import com.jsz.yun.ai.field.common.Rsp;
import com.jsz.yun.ai.field.entity.UserFileEntity;
import com.jsz.yun.ai.field.service.UserFileService;
import com.jsz.yun.ai.field.util.ThreadLocalSetting;
import com.jsz.yun.ai.field.vo.UserFileInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author gaoxing
 * @date 2025-03-28 15:52:17
 */
@Slf4j
@RestController
@RequestMapping("/file/")
public class UserFileController {

    @Autowired
    UserFileService userFileService;

    /**
     * 上传文件
     * @param file 文件信息
     * @param request 请求参数信息
     * @return 返回文件信息
     */
    @PostMapping("uploadFile")
    public <T> Rsp<UserFileInfoVO> parseValidate(@RequestPart("file") MultipartFile file, Req<T> request) {
        ThreadLocalSetting.USER_FILE_THREADLOCAL.set(UserFileEntity.saveUserInfo(request.getUserId()));
        UserFileEntity userFileInfoVO = userFileService.uploadFile(file);
        UserFileInfoVO fileInfoVO = new UserFileInfoVO();
        BeanUtil.copyProperties(userFileInfoVO, fileInfoVO);
        return Rsp.success(fileInfoVO,request);
    }
}
